
#include <stdlib.h>
#include <stdio.h>
#include "../common/sparse_matrix.h"
#include "../common/error.h"
#include "../common/mem.h"

int read_vector(int vector_size, elem* vector)
{
	int i;
	double d;

	for (i = 0; i < vector_size; i++)
	{
		if (scanf(elem_scan_format, &d) != 1)
			fatal_error("Bad value for vector");
		else
			vector[i] = d;
	}

	return 1;
}

void print_vector(int vector_size, const elem* vector)
{
	int i;

	for (i = 0; i < vector_size; i++)
	{
		printf("%f ", (double) vector[i]);
	}
}

int main()
{
	int n;
	sparse_matrix_arr* matrix;
	elem* vector;
	elem* result;

	scanf("%i", &n);

	if (n <= 0 || n > 1024)
		fatal_error("Bad value for matrix size, should be [1..1024].");

	vector = (elem*) mem_alloc_and_zero(n, sizeof(elem));
	result = (elem*) mem_alloc_and_zero(n, sizeof(elem));

	validate_allocation(vector, "input or input vector");
	validate_allocation(result, "input or result vector");

	matrix = sparse_matrix__read_from_stream(stdin, n);
	read_vector(n, vector);

	sparse_matrix__mlpl_matrix_times_vector(matrix, vector, result);

	print_vector(n, result);
	printf("\n");

	/*  TODO: Free allocated memory */

	return 0;
}